Page({
  data: {
    member_name: "",
    invitationCode: "",
    invitationUserId: "",
    member_phone: "",
    member_password: "",
    member_card: "",
    address: "",
    bank_card: "",
    pinless: "",
    btnValue: "获取验证码",
    verificationCode: "",
    second: 60,
    result: ""
  },
  getCode: function() {
    if (this.data.btnValue == "获取验证码") {
      this.data.btnValue = "60秒";
      let promise = new Promise((resolve, reject) => {
        let setTimer = setInterval(
          () => {
            var second = this.data.second - 1;
            this.setData({
              second: second,
              btnValue: second + '秒',
              btnDisabled: true
            });
            if (this.data.second <= 0) {
              this.setData({
                second: 60,
                btnValue: '获取验证码',
                btnDisabled: false
              })
              resolve(setTimer)
            }
          }, 1000)
      })
      promise.then((setTimer) => {
        clearInterval(setTimer)
      });
    }
  },


  //获得验证码
  getVerificationCode(e) {
    var reg = /^1[345789]\d{9}$/
    var member_phone = this.data.member_phone;
    if (member_phone == "") {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none',
        duration: 1500
      });
    } else if (!reg.test(member_phone)) {
      wx.showToast({
        title: '手机号码格式错误',
        icon: 'none',
        duration: 1500
      });
    } else {
      wx.request({
        url: 'http://localhost/selectByType',
        data: {
          "type": "member_phone",
          "typeValue": member_phone
        },
        type: "post",
        datatype: "json",
        success: function(data) {
          if (data.data == "存在") {
            wx.showToast({
              title: '该手机号码已经存在',
              icon: 'none',
              duration: 1500
            });
          }
        }
      });
    }
  },

  validateForm(e) {
    this.method.validateName(e.detail.member_name)

  },
  onLoad: function(options) {
    var that = this;
    that.setData({
      invitationUserId: 7,
      invitationCode: '54sdaa'
    });
  },
  //验证真实姓名
  validateName(e) {
    if (e != undefined) {
      this.data.member_name = e.detail.value;
    }
    var member_name = this.data.member_name;
    var reg = /^[u4E00-u9FA5]{2,4}$/;
    if (member_name == "") {
      wx.showToast({
        title: '请输入真实姓名',
        icon: 'none',
        duration: 1500
      });
      return false;
    } else if (reg.test(member_name)) {
      wx.showToast({
        title: '请输入两到四位的中文',
        icon: 'none',
        duration: 1500
      });
      return false;
    }
    return true;
  },
  //验证手机号码
  validatePhone(e) {
    this.data.result = "";
    if (e != undefined) {
      this.data.member_phone = e.detail.value;
    }
    var member_phone = this.data.member_phone;
    var reg = /^1[345789]\d{9}$/
    var flag = false;
    if (member_phone == "") {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none',
        duration: 1500
      });
      return false;
    } else if (!reg.test(member_phone)) {
      wx.showToast({
        title: '手机号码格式错误',
        icon: 'none',
        duration: 1500
      });
      return false;
    }
    return true;
  },
  //验证密码
  validatePassword(e) {
    if (e != undefined) {
      this.data.member_password = e.detail.value;
    }
    var member_password = this.data.member_password;
    if (member_password == "") {
      wx.showToast({
        title: '请输入密码',
        icon: 'none',
        duration: 1500
      });
      return false;
    }
    return true;
  },
  //验证身份证号码
  validateCard(e) {
    this.data.member_card = e.detail.value;
    var member_card = this.data.member_card;
    var reg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
    if (member_card == "") {
      wx.showToast({
        title: '请输入身份证号码',
        icon: 'none',
        duration: 1500
      });
    } else if (!reg.test(member_card)) {
      wx.showToast({
        title: '请输入正确格式的身份证号码',
        icon: 'none',
        duration: 1500
      });
    } else {
      wx.request({
        url: 'http://localhost/selectByType',
        data: {
          "type": "member_card",
          "typeValue": member_card
        },
        type: "post",
        datatype: "json",
        success: function(data) {
          if (data.data == "存在") {
            wx.showToast({
              title: '该身份证号码已经存在',
              icon: 'none',
              duration: 1500
            });
          }
        }
      });
    }
  },
  //验证详细地址
  validateAddress(e) {
    this.data.address = e.detail.value;
    var address = this.data.address;
    if (address == "") {
      wx.showToast({
        title: '请输入详细地址',
        icon: 'none',
        duration: 1500
      });
    }
  },
  //验证银行卡号
  validatebank(e) {
    this.data.bank_card = e.detail.value;
    var bank_card = this.data.bank_card;
    if (bank_card == "") {
      wx.showToast({
        title: '请输入银行卡号',
        icon: 'none',
        duration: 1500
      });
    } else if (!this.luhmCheck(bank_card)) {
      wx.showToast({
        title: '银行卡号格式不对',
        icon: 'none',
        duration: 1500
      });
    } else {
      wx.request({
        url: 'http://localhost/selectByType',
        data: {
          "type": "bank_card",
          "typeValue": bank_card
        },
        type: "post",
        datatype: "json",
        success: function(data) {
          if (data.data == "存在") {
            wx.showToast({
              title: '该银行卡号已经存在',
              icon: 'none',
              duration: 1500
            });
          }
        }
      });
    }
  },
  //验证支付宝账号
  validatePinless(e) {
    this.data.pinless = e.detail.value;
    var pinless = this.data.pinless;
    if (pinless == "") {
      wx.showToast({
        title: '请输入支付宝账号',
        icon: 'none',
        duration: 1500
      });
    } else {
      wx.request({
        url: 'http://localhost/selectByType',
        data: {
          "type": "pinless",
          "typeValue": pinless
        },
        type: "post",
        datatype: "json",
        success: function(data) {
          if (data.data == "存在") {
            wx.showToast({
              title: '该支付宝账号已经存在',
              icon: 'none',
              duration: 1500
            });
          }
        }
      });
    }
  },
  //验证注册
  validateForm(e) {
    var data = this.data;
    var that = this;
    if (this.validateName() && this.validatePhone()) {
      this.selectByType("member_phone", data.member_phone).then((res) => {
        if (this.data.result == "存在") {
          wx.showToast({
            title: '该手机号码已存在',
            icon: 'none',
            duration: 1500
          });
        } else if (that.validatePassword()) {
          console.log("2");
        }
      });
    }

    /*var member_name = e.detail.value.member_name;
    var reg = /^[u4E00-u9FA5]{2,4}$/;
    var member_phone = e.detail.value.member_phone;
    var regPhone = /^1[345789]\d{9}$/;
    var member_password = e.detail.value.member_password;
    var regCard = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
    var member_card = e.detail.value.member_card;
    var address = e.detail.value.address;
    var bank_card = e.detail.value.bank_card;
    var pinless = e.detail.value.pinless;
    if (member_name == "") {
      wx.showToast({
        title: '请输入真实姓名',
        icon: 'none',
        duration: 1500
      });
    } else if (reg.test(member_name)) {
      wx.showToast({
        title: '请输入两到四位的中文',
        icon: 'none',
        duration: 1500
      });
    } else if (member_phone == "") {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none',
        duration: 1500
      });
    } else if (!regPhone.test(member_phone)) {
      wx.showToast({
        title: '手机号码格式错误',
        icon: 'none',
        duration: 1500
      });
    } else {
      wx.request({
        url: 'http://192.168.0.100/selectByType',
        data: {
          "type": "member_phone",
          "typeValue": member_phone
        },
        type: "post",
        datatype: "json",
        success: function(data) {
          if (data.data == "存在") {
            wx.showToast({
              title: '该手机号码已经存在',
              icon: 'none',
              duration: 1500
            });
          } else if (member_password == "") {
            wx.showToast({
              title: '请输入密码',
              icon: 'none',
              duration: 1500
            });
          } else if (member_card == "") {
            wx.showToast({
              title: '请输入身份证号码',
              icon: 'none',
              duration: 1500
            });
          } else if (!regCard.test(member_card)) {
            wx.showToast({
              title: '请输入正确格式的身份证号码',
              icon: 'none',
              duration: 1500
            });
          } else {
            wx.request({
              url: 'http://192.168.0.100/selectByType',
              data: {
                "type": "member_card",
                "typeValue": member_card
              },
              type: "post",
              datatype: "json",
              success: function(data) {
                if (data.data == "存在") {
                  wx.showToast({
                    title: '该身份证号已经存在',
                    icon: 'none',
                    duration: 1500
                  });
                } else if (address == "") {
                  wx.showToast({
                    title: '请输入详细地址',
                    icon: 'none',
                    duration: 1500
                  });
                } else if (bank_card == "") {
                  wx.showToast({
                    title: '请输入银行卡号',
                    icon: 'none',
                    duration: 1500
                  });
                } else {
                  wx.request({
                    url: 'http://192.168.0.100/selectByType',
                    data: {
                      "type": "bank_card",
                      "typeValue": bank_card
                    },
                    type: "post",
                    datatype: "json",
                    success: function(data) {
                      if (data.data == "存在") {
                        wx.showToast({
                          title: '该银行卡号已经存在',
                          icon: 'none',
                          duration: 1500
                        });
                      } else if (pinless == "") {
                        wx.showToast({
                          title: '请输入支付宝账号',
                          icon: 'none',
                          duration: 1500
                        });
                      } else {
                        wx.request({
                          url: 'http://192.168.0.100/selectByType',
                          data: {
                            "type": "pinless",
                            "typeValue": pinless
                          },
                          type: "post",
                          datatype: "json",
                          success: function(data) {
                            if (data.data == "存在") {
                              wx.showToast({
                                title: '该支付宝账号已经存在',
                                icon: 'none',
                                duration: 1500
                              });
                            } else {
                              wx.request({
                                url: 'http://192.168.0.100/addMember',
                                data: {
                                  "member_name": member_name,
                                  "member_phone": member_phone,
                                  "member_password": member_password,
                                  "member_card": member_card,
                                  "address": address,
                                  "bank_card": bank_card,
                                  "pinless": pinless,
                                  "invitationUserId": '7',
                                  "invitationCode": '895sdc'
                                },
                                type: "post",
                                datatype: "json",
                                success: function(data) {
                                  wx.reLaunch({
                                    url: '../index/index',
                                  });
                                }
                              });
                            }
                          }
                        });
                      }
                    }
                  });
                }
              }
            });
          }
        }
      });
    }*/
  },
  selectByType(type, typeValue) {
    var that = this;
    return new Promise((resolve, reject) => {
      wx.request({
        url: "http://localhost/selectByType",
        method: 'post',
        data: {
          "type": type,
          "typeValue": typeValue
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        success(data) {
          that.data.result = data.data;
          resolve();
        }
      });
    });
  },
  luhmCheck(bankno) {
    bankno = bankno.replace(/\s/ig, '');
    var lastNum = bankno.substring(bankno.length - 1, bankno.length); //取出最后一位（与luhm进行比较）
    var first15Num = bankno.substring(0, bankno.length - 1); //前15或18位
    var newArr = new Array();
    var sumTotal = 0;
    for (var i = first15Num.length - 1, j = 0; i >= 0; i--, j++) {
      var yuansu = parseInt(first15Num[i]);
      if (j % 2 == 0) {
        yuansu *= 2;
        yuansu = parseInt(yuansu / 10) + parseInt(yuansu % 10);
      }
      sumTotal += parseInt(yuansu);
    }

    //计算Luhm值
    var k = parseInt(sumTotal) % 10 == 0 ? 0 : 10 - parseInt(sumTotal) % 10;
    if (lastNum == k) {
      return true;
    } else {

      return false;
    }          
  }

});